//
//  JSMLeavingHeaderScollViewWebController.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMLeavingHeaderScollViewWebController.h"
#import "ShareView.h"
@interface JSMLeavingHeaderScollViewWebController ()<UIWebViewDelegate>
@property(nonatomic, strong) UIWebView *webView;
@property(nonatomic, strong) MBProgressHUD *webHud;
@property(nonatomic, strong) UIActivityIndicatorView *aiView;
/**
 *  分享标题
 */
@property (nonatomic, strong) NSString *shareTitle;

/**
 *  分享内容
 */
@property (nonatomic, strong) NSString *shareText;

/**
 *  分享链接地址
 */
@property (nonatomic, strong) NSString *shareUrl;
@property(nonatomic, strong) ShareView *shareView;
@end

@implementation JSMLeavingHeaderScollViewWebController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    BaseImageView *backgoundImageView = [[BaseImageView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
    backgoundImageView.image = [UIImage imageNamed:@""]; // 背景图片
    [self.view addSubview:backgoundImageView];
    [self createWebView];
    [self createShareButton];
}
#pragma mark 分享
- (void)createShareButton {
    BaseButton *sharebutton = [BaseButton buttonWithType:UIButtonTypeCustom];
    sharebutton.frame = CGRectMake(0, 0, 40, 40);
    [sharebutton setImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:sharebutton];
    [sharebutton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)shareButtonAction:(BaseButton *)button {
    if (!self.shareView) {
        self.shareView = [[ShareView alloc] initWithFrame:self.view.bounds];
        self.shareView.alpha = 0.0;
        [self.view addSubview:self.shareView];
        self.shareView.shareTitle = [NSString stringWithFormat:@"标题:%@", self.headerScollModel.title];
        
        self.shareView.shareText = NSLocalizedString(@"分享内容", nil);
        
        self.shareView.shareUrl = [NSString stringWithFormat:@"%@", self.headerScollModel.url];
        
        [UIView animateWithDuration:1 animations:^{
            self.shareView.alpha = 1.0;
        }];
        
        
    }else{
        [UIView animateWithDuration:1 animations:^{
            self.shareView.alpha = 1.0;
        }];
        
    }
   
}

- (void)shareToWeiboBase
{
    
    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
    authRequest.redirectURI = @"http://www.sina.com";
    authRequest.scope = @"all";
    
    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare] authInfo:authRequest access_token:nil];
    
    request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    
    [WeiboSDK sendRequest:request];
    
}
- (WBMessageObject *)messageToShare
{
    
    WBMessageObject *message = [WBMessageObject message];
    BOOL hadInstalledWeibo = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weibo://"]];
    
    if(hadInstalledWeibo){
        WBWebpageObject *webpage = [WBWebpageObject object];
        webpage.objectID = @"identifier1";
        webpage.title = self.shareTitle;
        webpage.description = self.shareText;
        //  可改为自定义图片
        webpage.thumbnailData = UIImageJPEGRepresentation([UIImage imageNamed:@"logo.jpg"], 0.5);
        webpage.webpageUrl = self.shareUrl;
        
        message.mediaObject = webpage;
    }
    
    message.text = [NSString stringWithFormat:@"%@ - %@ - %@", self.shareTitle, self.shareText, self.shareUrl];
    
    
    return message;
    
}
+ (instancetype)sharedInstance
{
    
    static JSMLeavingHeaderScollViewWebController* util;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        util = [[JSMLeavingHeaderScollViewWebController alloc] init];
        
    });
    return util;
    
}

- (instancetype)init
{
    
    static id obj=nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        obj = [super init];
        if (obj) {
            
        }
        
    });
    self=obj;
    return self;
    
}



- (void)createWebView {
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
    [self.view addSubview:self.webView];
    self.webHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.webHud.labelText = @"Loading...";
    [self loadWebData];
    self.webView.scrollView.bounces = NO;
    self.webView.scalesPageToFit = YES;
    self.webView.delegate = self;
}

- (void)loadWebData {
    NSURL *url = [NSURL URLWithString:self.headerScollModel.url];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    
}

- (void)webViewDidStartLoad:(UIWebView *)webView {

}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.webHud removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
